var form, table, tableInstance;

$(function () {
    initDict(function () {
        initLayUI(function () {
            initButtons();
        })
    });
});

function initDict(cb) {
    ajaxUtil.loadDict("sys_device_type", function (data) {
        data.forEach(function (item) {
            let optionStr = "<option value='" + item.dictValue + "' ";
            if (item.isDefault) {
                optionStr += " selected ";
            }
            optionStr += ">" + item.dictLabel + "</option>";
            $("#device").append(optionStr);
        });

        cb();
    });
}

function initLayUI(cb) {
    layui.use(['table', 'form'], function () {
        table = layui.table;
        form = layui.form;

        form.render();
        let data = form.val('form');

        tableInstance = table.render({
            elem: '#tokenTable',
            url: ServerURL + "/token/list",
            headers: tokenUtil.getAuthHeader(),
            method: "POST",
            limits: [10, 15, 20],
            cols: [[
                {field: 'token', title: 'Token', align: 'center', minWidth: 300},
                {field: 'outTime', title: '到期时间', align: 'center', minWidth: 100},
                {field: 'userName', title: '用户昵称', align: 'center', minWidth: 50},
                {field: 'loginAccount', title: '登录账号', align: 'center', minWidth: 100},
                {field: 'device', title: '所属应用', align: 'center', minWidth: 100},
                {field: 'roleName', title: '角色类型', align: 'center', minWidth: 100},
                {field: 'oper', title: '操作', minWidth: 100, align: 'center', toolbar: "#operBar"}
            ]],
            contentType: 'application/json',
            response: {
                statusName: 'code',
                statusCode: 200,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            },
            where: data,
            loading: true,
            page: true,
            error: function (res) {
                ajaxUtil.handleError(res);
            }
        });

        table.on('tool()', function (obj) {
            const data = obj.data;
            if (obj.event === 'delete') {
                deleteToken(data)
            }
        });

        cb();
    });
}

function initButtons() {
    $("#searchBtn").click(function () {
        reloadTable();
    });

    $("#resetBtn").click(function () {
        form.val('form', {
            "loginAccount": "",
        });
        reloadTable();
    });

}

function reloadTable() {
    let data = form.val('form');
    tableInstance.reload({
        where: data,
        page: {
            curr: 1
        }
    });
}

function deleteToken(data) {
    layerUtil.confirm("确定要注销该Token么?", function () {
        ajaxUtil.post({
            token: data.token,
            device: data.device
        }, "/token/delete", function (res) {
            if (res.code === 200) {
                layerUtil.success("注销成功", function () {
                    reloadTable();
                });
            } else {
                layerUtil.error(res.msg);
            }
        });
    });
}